package cn.zn.modules.zzsys.aspect;

import cn.zn.framework.common.utils.Constant;
import cn.zn.framework.common.utils.HttpContextUtils;
import cn.zn.framework.common.utils.IPUtils;
import cn.zn.framework.redis.RedisUtils;
import cn.zn.modules.zzsys.cur.entity.OperLog;
import cn.zn.modules.zzsys.inter.annotation.SysLog;
import cn.zn.modules.zzsys.inter.dto.LoginUserDTO;
import cn.zn.modules.zzsys.cur.service.OperLogService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


/**
 * 系统日志，切面处理类
 */
@Aspect
@Component
@Slf4j
public class SysLogAspect {
    @Autowired
    private OperLogService operLogService;
    @Autowired
    private RedisUtils redisUtils;

    @Pointcut("@annotation(cn.zn.modules.zzsys.inter.annotation.SysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        //保存日志
        saveSysLog(point, time, result);

        return result;
    }

    private void saveSysLog(ProceedingJoinPoint joinPoint, long time, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        OperLog operLog = new OperLog();
        SysLog syslog = method.getAnnotation(SysLog.class);
        if (syslog != null) {
            //注解上的描述
            operLog.setOperation(syslog.value());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        operLog.setMethod(className + "." + methodName + "()");

        //请求的参数
        Object[] args = joinPoint.getArgs();
        try {
            String params = JSON.toJSONString(args);
            operLog.setParams(params);
        } catch (Exception e) {

        }

        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        operLog.setIp(IPUtils.getIpAddr(request));
        operLog.setTime(time);
        //用户名
        Object token = request.getAttribute("token");
        log.debug("token:{}", token);
        if(token != null){
            //获取用户信息
            LoginUserDTO loginUser = redisUtils.hget(Constant.REDIS_LOGIN_USER, token.toString(), LoginUserDTO.class);
            if(loginUser != null){
                operLog.setUserCode(loginUser.getWorkNo());
                operLog.setUserName(loginUser.getName());
            }
        }
        // 执行结果
        if(result != null){
            operLog.setResult(result.toString());
        }
        //保存系统日志
        operLogService.save(operLog);
    }
}
